package controller;

import validate.RecipeCommentValidate;
import model.RecipeComment;
import model.User;

import com.jfinal.core.Controller;

import constant.Msg;

public class RecipeCommentController extends Controller{
	public void index(){
		String recipeId=getPara("id");
		String content=getPara("content");
		String commentId=getPara("comment_id");
		String valid=null;
		int userId=User.userCache.get(getHeader("token"));
		switch (getRequest().getMethod()) {
		case "GET":
			valid=RecipeCommentValidate.get(recipeId);
			if(valid!=null){
				renderJson(valid);
				return;
			}
			renderJson(RecipeComment.dao.showComments(Integer.parseInt(recipeId)));
			break;
		case "POST":
			RecipeComment comment=new RecipeComment();
			comment.set("user_id", userId);
			comment.set("recipe_id", recipeId);
			comment.set("content", content);
			valid=RecipeCommentValidate.post(comment);
			if (valid!=null) {
				renderJson(valid);
				return;
			}
			renderJson(RecipeComment.dao.addComment(comment));
			break;
		case "DELETE":
			valid=RecipeCommentValidate.delete(commentId, userId);
			if(valid!=null){
				renderJson(valid);
				return;
			}
			renderJson(RecipeComment.dao.deleteComment(commentId));
			break;
		default:
			renderJson(Msg.SUCCESS_EXECUTE);
			break;
		}
	}
}
